<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {
	public function __construct() {
		parent::__construct();
		$this->load->helper('public');
		if (!isset($_SESSION['passid']) || empty($_SESSION['passid'])) {
			show_message('请登录', site_url('user/login').'?goto='.current_url());
			exit();
		}
		$this->load->library('form_validation');
		//设置表单错误分隔符，设置为空，直接输出即可
		$this->form_validation->set_error_delimiters('','');
		$this->load->model('member_model', 'member');
		$this->load->model('bbs_model', 'bbs');
		error_reporting(E_ALL);
		$this->load->helper('public');
		$this->load->helper('ip');
		
	}
	
	public function createrole() {
		$config = array(
				array(
						'field' => 'rolename',
						'label' => '论坛名',
						'rules' => 'tirm|required'
						)
				);
		$this->form_validation->set_rules($config);
		$this->form_validation->set_message('required', '<i class="icon-exclamation-sign"></i>此项必填');
		if ($this->form_validation->run() === FALSE) {
			$this->load->view('/user/createrole');
		} else {
			$rolename = $this->input->post('rolename');
			$member = $this->member->select_one_by_field('id', $_SESSION['passid']);
			$roles_num = $this->member->get_roles_num($_SESSION['passid']);
			
			if ($member['adminid'] == '0' && $roles_num > 0) {
				show_error('管理员才有创建马甲的权力');
			}
			
			$isDefault = $roles_num == 0?1:0;

			$uc_id = uc_user_register($rolename, authcode($member["password"], "DECODE", $this->config->item("password_encode_key")),$member['email']);
			
			if ($uc_id == -1) {
				$data['error'] = '用户名不合法';
				$this->load->view('/user/createrole', $data);
				return;
			}
			
			if ($uc_id == -2) {
				$data['error'] = '包含不允许注册的词语';
				$this->load->view('/user/createrole', $data);
				return;
			}
			
			if ($uc_id == -3) {
				$data['error'] = '用户名已经存在';
				$this->load->view('/user/createrole', $data);
				return;
			}
			
			if ($uc_id == -4) {
				$data['error'] = 'Email 格式有误';
				$this->load->view('/user/createrole', $data);
				return;
			}
			
			if ($uc_id == -5) {
				$data['error'] = 'Email 不允许注册';
				$this->load->view('/user/createrole', $data);
				return;
			}
			
			if ($uc_id == -6) {
				$data['error'] = '该 Email 已经被注册';
				$this->load->view('/user/createrole', $data);
				return;
			}
			
			
			$roledata = array(
					'pass_id' => $_SESSION['passid'],
					'uc_id' => $uc_id,
					'uc_name' => $rolename,
					'uc_password' => $member["password"],
					'regdate' => time(),
					'isdefault' => $isDefault
					);
			$roleId = $this->member->create_role($roledata);
			
			if (empty($roleId)) {
				show_error('创建用户名失败，请联系管理员');
			}

			$current_time = time();

			$common_member = array(
				"uid" => $uc_id,
				"username" => $rolename,
				"password" => md5(rand(1000,99999999)),
				"adminid" => '0',
				"groupid" => '10',
				"regdate" => $current_time,
				"timeoffset" => "9999",
				"email" => $member['email']
				);
			$this->bbs->insert("common_member", $common_member);

			$common_memberfields = array(
				"uid" => $uc_id
				);
			$this->bbs->insert("common_member_profile", $common_memberfields);

			$memberStatus = array(
				"uid" => $uc_id,
				"regip" => get_client_ip()
				);
			$this->bbs->insert("common_member_status", $memberStatus);

			$memberFieldForum = array(
				"uid" => $uc_id
				);
			$this->bbs->insert("common_member_field_forum", $memberFieldForum);

			$memberFieldHome = array(
				"uid" => $uc_id
				);
			$this->bbs->insert("common_member_field_home", $memberFieldHome);

			$memberCount = array(
				"uid" => $uc_id
				);
			$this->bbs->insert("common_member_count", $memberCount);

			$synLoginCode = uc_user_synlogin($uc_id);
			show_message('登录成功'.$synLoginCode, $this->config->item('bbs_path'));
		}
	}
	
	public function test() {
		$roles = $this->member->get_roles(1);
		
		if (empty($roles)) {
			echo 'empty';
		}
	}

	public function lib() {
		$config = array(
			array(
				"field" => "password",
				"label" => "password",
				"rules" => "trim|required"
				)
			);
		$this->form_validation->set_rules($config);
		$this->form_validation->set_message('required', '<i class="icon-exclamation-sign"></i>此项必填');

		if ($this->form_validation->run() == FALSE) {
			$this->load->view('home/lib');
		} else {
			$password = $this->input->post("password");

			if (!$this->check_lib_password($_SESSION["stuID"], $password)) {
				unset($password);
				unset($encodePassword);
				$view['success'] = "绑定成功";
				$this->load->view("/home/lib", $view);
				return ;
			}

			$encodePassword = authcode($password, "encode", $this->config->item("password_encode_key"));

			if ($this->member->bind_lib_account($encodePassword)) {
				unset($password);
				unset($encodePassword);
				$view['success'] = "绑定成功";
				$this->load->view("/home/lib", $view);
				//echo "success";
			} else {
				show_error("图书馆绑定失败，插入数据库失败");
			}
		}
	}
	/*
		验证图书馆账号密码
	 */
	protected function check_lib_password($stuID, $password) {
		return true;
	}
}